package phonedate;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @program: WCount
 * @description: 序列化的reduce阶段
 * @author: Qiang.Ye
 * @create: 2019-05-26 19:39
 **/
public class FlowCountReducer extends Reducer<Text,FlowBean,Text,FlowBean> {

    Text k = new Text();



    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        //上传和下载的总和初始化
        long    sum_upFlow = 0;
        long    sum_downFlow = 0;

        //1.遍历所有bean，将其中的上行流量，下行流量分别累加
        for(FlowBean bean : values){

            sum_upFlow += bean.getUpFlow();
            sum_downFlow += bean.getDownFlow();

        }
        //2.封装对象
        FlowBean resultBean = new FlowBean(sum_upFlow, sum_downFlow);
        k.set(key);
        //3.写出
        context.write(k,resultBean);

    }
}
